package cn.xm.basic.service.impl;

import cn.xm.basic.constants.Constant;
import cn.xm.basic.dto.BindUserDto;
import cn.xm.basic.dto.PhoneDto;
import cn.xm.basic.exception.BusinessException;
import cn.xm.basic.result.AjaxResult;
import cn.xm.basic.service.VerifyCodeService;
import cn.xm.basic.utils.StrUtils;
import cn.xm.basic.utils.VerifyCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import sun.security.pkcs11.wrapper.Constants;

import java.util.concurrent.TimeUnit;

@Service
public class VerifyCodeServiceImpl implements VerifyCodeService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public String getImage(String imageCodeKey) {
        // 生成验证码的值
        String imageCodeValue = StrUtils.getComplexRandomString(4);
        // 存到redis,有效时间为5分钟
        redisTemplate.opsForValue().set(imageCodeKey,imageCodeValue,5L, TimeUnit.MINUTES);
        // 生成图片
        String imageEncode = VerifyCodeUtils.VerifyCode(100, 40, imageCodeValue);
        return imageEncode;
    }
    @Override
    public void sendSmscode(PhoneDto phoneDto) {
        // 参数校验
        // 从redis获取图片验证码
        String imageCodeValue = (String) redisTemplate.opsForValue().get(phoneDto.getImageCodeKey());
        // 如果没有就抛出异常
        if (StringUtils.isEmpty(imageCodeValue)) {
            throw new BusinessException("图片验证码不存在");
        }
        if (!imageCodeValue.equalsIgnoreCase(phoneDto.getImageCode())) {
            throw new BusinessException("图片验证码错误");
        }
        /**
         * 获取短信验证码
         */
        String phone = phoneDto.getPhone();
        getSmsCode(phone);


    }

    /**
     * 绑定获取短信验证码
     * @param
     */
    @Override
    public void smsCode(String phone) {
        getSmsCode(phone);
    }

    private void getSmsCode(String phone) {
        phone = phone.split("=")[0];
        // 获取短信验证码
        String smsCodeKey = phone + ":" + Constant.REGISTER_NAME;
        // 从redis获取短信验证码
        String smsCodeValue = (String)redisTemplate.opsForValue().get(smsCodeKey);
        String onlyValue = "";
        // 如果redis不存在可能是过期或者第一次获取短信验证码
        if (StringUtils.isEmpty(smsCodeValue)) {
            // 创建短信验证码并发送
            // 短信验证码的值，需要带上当前时间
            onlyValue = StrUtils.getComplexRandomString(16);
            System.out.println("onlyValue= " + onlyValue);
        } else {
            // 发送短信间隔时间不得少于一分钟
            if ((System.currentTimeMillis() - Long.valueOf(smsCodeValue.split(":")[1])) <= 1*60*1000) {
                throw new BusinessException("发送过于频繁，请一分钟后再试");
            }
            // 重新发送刚刚的短信,并且从保存到redis以刷新时间
            onlyValue = smsCodeValue.split(":")[0];
            System.out.println(onlyValue);
        }
        smsCodeValue = onlyValue + ":" + System.currentTimeMillis();
        // 存储短信验证码到redis
        redisTemplate.opsForValue().set(smsCodeKey,smsCodeValue,Constant.SMSCODE_TIMEOUT,TimeUnit.MINUTES);
    }
}
